package com.qfhd.soft.cjzcore.Core;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

//范例(图片高斯模糊)
//Bitmap bitmap   = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.jianzhu_1);
//        bitmap = V2BlurUtils.blurBitmap(this, bitmap, 16);
//        bitmap = V2BlurUtils.cropBitmap(bitmap, QMUIDisplayHelper.getScreenWidth(mContext), QMUIDisplayHelper.getScreenHeight(mContext));
//        backgroundLayout.setBackground(new BitmapDrawable(getResources(),bitmap));

public class V2BlurUtils {
    private static final float BITMAP_SCALE = 0.4f;

    public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
        int width = Math.round(image.getWidth() * BITMAP_SCALE);
        int height = Math.round(image.getHeight() * BITMAP_SCALE);
        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        blurScript.setRadius(blurRadius);
        blurScript.setInput(tmpIn);
        blurScript.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
        return outputBitmap;
    }

    public static Bitmap scaleBitmap(Bitmap origin, float ratio) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(ratio, ratio);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        origin.recycle();
        return newBM;
    }

    public static Bitmap cropBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth(); // 得到图片的宽，高
        int h = bitmap.getHeight();

        double bp = (w * 1.0) / h;
        double np = (width * 1.0) / height;

        int scaleWidth = width;
        int scaleHeight = height;
        float scale = 0.0f;

        if (bp > np) {
            scaleHeight = height;
            scaleWidth = (int) ((scaleHeight * w * 1.0) / h);
        } else {
            scaleWidth = width;
            scaleHeight = (int) ((scaleWidth * h * 1.0) / w);
        }

        scale = (float) ((scaleWidth * 1.0) / w);
        bitmap = scaleBitmap(bitmap, scale);
        scaleWidth = bitmap.getWidth();
        scaleHeight = bitmap.getHeight();

        int objWidth = (scaleWidth >= width) ? width : scaleWidth;
        int objHeight = (scaleHeight >= height) ? height : scaleHeight;
        return Bitmap.createBitmap(bitmap, (scaleWidth - width) / 2, (scaleHeight - height) / 2, objWidth, objHeight, null, false);
    }
}
